package b2.widget
{
	import b2.net.*;
	import b2.scene.*;
	import b2.util.*;
	
	import com.greensock.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	import flash.utils.*;

	public class InProcessWindow extends Sprite
	{
		static private var instance:InProcessWindow;
		//正在处理文字
		private var processingStr:String;
		//正在处理文本
		private var processingText:TextField;
		//最长显示时间（单位：秒）
		private const MAX_SHOW_TIME:int = 20;
		//显示的时间（冷却计数，单位：秒）
		private var showTimeout:int;
		//显示时钟
		private var timer:Timer;
		
		public function InProcessWindow()
		{
			super();
			//透明背景
			var bgr:Sprite = new Sprite();
			var gfx:Graphics = bgr.graphics;
			gfx.beginFill(0x000000, 0.01);
			gfx.drawRect(0, 0, BFont.stageWidth, BFont.stageHeight);
			gfx.endFill();
			addChild(bgr);
			//白色框
			gfx.beginFill(0xFFFFFF, 0.8);
			gfx.drawRoundRect(250, 270, 320, 40, 10, 10);
			gfx.endFill();
			addChild(bgr);
			//正在处理文字
			processingText = new TextField();
			processingText.selectable = false;
        	processingText.autoSize = TextFieldAutoSize.LEFT;
			processingText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000, true);
			processingText.filters = [new GlowFilter(0xFFFFFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			processingText.x = 300;
			processingText.y = 280;
			addChild(processingText);
			//动画
			var InProcess:Class = ResourceHelper.getInstance().getClassByName("InProcess");
			var inProcess:BasicMc = new BasicMc(InProcess, 20);
			inProcess.x = 150;
			inProcess.y = 158;
			inProcess.scaleX = 1.4;
			inProcess.scaleY = inProcess.scaleX;
			addChild(inProcess);
			
			this.mouseEnabled = false;
			timer = new Timer(1000);
			timer.addEventListener(TimerEvent.TIMER, onTick);
		}
		
		static public function getInstance():InProcessWindow
		{
			if (!instance)
				instance = new InProcessWindow();
			return instance;
		}
		
		private function onTick(event:TimerEvent):void
		{
			processingText.text = processingStr;
			for (var i:int = 0; i < (MAX_SHOW_TIME - showTimeout) % 4; i++)
				processingText.appendText(" .");
			showTimeout--;
			if (showTimeout <= 0)
				hide();
		}
		
		public function show(text:String):void
		{
			showTimeout = MAX_SHOW_TIME;
			processingStr = text;
			processingText.text = text;
			if (this.parent == null)
			{
				processingText.x = 400 - processingText.width / 2;
				SceneManager.getInstance().currentScene.addChild(this);
				timer.start();
			}
		}
		
		public function hide():void
		{
			if (this.parent != null)
			{
				this.parent.removeChild(this);
				timer.stop();
			}
		}
		
	}
}